﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Domain.PermisosModule.Contracts.Areas;
using Cesave.Infrastructure.CrossCutting.IoC;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;
using Cesave.Domain.Core.Entities;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Areas
{
	public partial class AreasViewModel
	{
		private List<Area> _areas;
		private Area _area;

		public List<Area> Areas
		{
			get
			{
				return _areas;
			}
			private set
			{
				_areas = value;
				if (_areas != null)
					RaisePropertyChanged("Areas");
			}
		}

		public Area Area
		{
			get
			{
				return _area;
			}
			private set
			{
				_area = value;
				if (_area != null)
					RaisePropertyChanged("Area");
			}
		}

		private void AddArea(object sender, EventArgs e)
		{
			_areasView.areaBindingSource.AddingNew += AddingNewArea;
			_areasView.areaBindingSource.AddNew();
		}

		private void AddingNewArea(object sender, AddingNewEventArgs e)
		{
			Area = new Area
			{
				AreaGuid = Guid.NewGuid(),
				AreaId = 0,
				JuntaGuid = Junta.JuntaGuid,
				JuntaId = Junta.JuntaId,
				Letra = string.Empty,
				Nombre = string.Empty,
				MostrarEnExpedicion = true,
				Status = true,
			};
			Area.MarkAsAdded();
			e.NewObject = Area;
			FocusDefaultControl();
		}

		private void LoadAreas()
		{
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IAreaService areaService = IoCFactory.Resolve<IAreaService>();
					e.Result = areaService.FindAreasByJunta(Junta.JuntaGuid);
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					if (!e.Cancelled && e.Error == null)
						Areas = e.Result as List<Area>;
					else if (e.Error != null)
						MessageBox.Show(_areasView, e.Error.Message, WindowTitles.AreasWindowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		private void SetCurrentArea(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if (bindingSource != null)
				if (bindingSource.Current != null)
					Area = bindingSource.Current as Area;
		}

		private void OnAreasPropertyChanged()
		{
			_areasView.areaBindingSource.DataSource = Areas;
			_areasView.areaBindingSource.CurrentChanged += SetCurrentArea;
			SetCurrentArea(_areasView.areaBindingSource, new EventArgs());
		}

		private void OnAreaPropertyChanged()
		{

		}

		private void DeleteArea(object sender, EventArgs e)
		{
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (s, args) =>
				{
					IAreaService areaService = IoCFactory.Resolve<IAreaService>();
					areaService.DeleteArea(Area);
				};
				worker.RunWorkerCompleted += (s, args) =>
				{
					if (!args.Cancelled && args.Error == null)
						LoadAreas();
					else if (args.Error != null)
						MessageBox.Show(_areasView, args.Error.Message, WindowTitles.AreasWindowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		private void SaveArea(object sender, EventArgs e)
		{
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (s, arg) =>
				{
					IAreaService areaService = IoCFactory.Resolve<IAreaService>();
					if (Area.ChangeTracker.State == ObjectState.Added)
						areaService.AddArea(Area);
					else
						areaService.ChangeArea(Area);
					Area.AcceptChanges();
				};
				worker.RunWorkerCompleted += (s, arg) =>
				{
					if (!arg.Cancelled && arg.Error == null)
					{
						_areasView.areaBindingSource.RemoveCurrent();
						LoadAreas();
					}
					else
						MessageBox.Show(_areasView, arg.Error.Message, WindowTitles.AreasWindowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}
	}
}
